//
//  GeneralCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/1/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit





final class GeneralCell: UITableViewCell, ConfigurableCell {
    
    // MARK: - Outlets
    
    lazy private var regionField: ParametersView = {
        let view = ParametersView()
        view.delegate = self
        view.items = [ParametersViewModel(input: Input(type: "region",
                                             value: nil, placeholder: "Область", icon: nil, values: nil))]
        view.setBackground(color: .whiteBlueColor)
        view.setCorner(4.0)
        return view
    }()

    lazy private var transportFeatureSegmented: RoundedSCView = {
        let segmentedView = RoundedSCView()
        segmentedView.updateItems(items: TransportFeature.allCases.map{ $0.rawValue })
        return segmentedView
    }()
    
    // MARK: - Properties
    
    private var index: Int!
    private var data: Filter.FilterGeneral! {
        didSet {
            self.setRegion()
        }
    }
    
    // MARK: - Actions
    
    @objc private func getDataFromRoot (notification: NSNotification) {
        if let userInfo = notification.userInfo {
            let type = userInfo["type"] as! String
            if type == "region" {
                self.data.region = userInfo["data"] as? [City]
            }
        }
    }
    
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
        NotificationCenter.default.addObserver(self, selector: #selector(getDataFromRoot(notification:)), name: NSNotification.Name(rawValue: "setGeneral"), object: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // MARK: - Methods
    
    func configure(data object: Filter.FilterGeneral, index: Int) {
        self.index = index
        self.data = object
        self.transportFeatureSegmented.transportSelect.selectedSegmentIndex = TransportFeature.allCases.firstIndex(of: TransportFeature(rawValue: (data!.transportFeature) ?? TransportFeature.all.rawValue)!)!
    }
    func returnData() -> Filter.FilterGeneral {
        return Filter.FilterGeneral(region: self.data.region, transportFeature: TransportFeature.allCases[transportFeatureSegmented.transportSelect.selectedSegmentIndex].rawValue)
    }
}

// MARK: - Methods

extension GeneralCell {
    private func setRegion () {
        var regionTxt: String?
        if let region = self.data.region {
            if region.count > 1 {
                let txt = region.map {
                    $0.name
                }.joined(separator: ", ")
                regionTxt = txt
            } else if region.count == 1 {
                regionTxt = region[0].name
            }
        }
        self.regionField.items = [
            ParametersViewModel(input: Input(type: "region", value: regionTxt, placeholder: "Область"))
        ]
        regionField.setBackground(color: .whiteBlueColor)
    }
}


// MARK: - Adding views to superview

extension GeneralCell {
    
    private func addViews () {
        self.contentView.addSubview(regionField)
        self.contentView.addSubview(transportFeatureSegmented)
        self.applyConstraints()
    }
    
    private func applyConstraints() {
        regionField.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().inset(8)
            make.height.equalTo(48)
        }
        transportFeatureSegmented.snp.makeConstraints { (make) in
            make.top.equalTo(regionField.snp.bottom).offset(16)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(40)
            make.bottom.equalToSuperview().offset(-20)
        }
    }
}

extension GeneralCell: ParametersViewDelegate {
    
    func didSelectParameter(item: ParametersViewModel) {
        if item.type == "region" {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "handleCell"), object: nil, userInfo: [
                    "cell": self,
                    "type": "region"
                ])
        }
    }
    
    func didClearValue(value: String?, placeholder: String, itemIndex: Int) {
        if (placeholder == "Область") {
            self.data.region = nil
        }
    }
    
}
